blog

Home / DeveloperSection / Blogs / LINQ to SQL Backward compatibility:

LINQ to SQL Backward compatibility:

Gaurav Shukla6273 05-Jan-2012
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Data.SqlClient;

namespace GetCommandLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            DataContext oDataContext = new DataContext("data source=WRHNDEV03\\SQLEXPRESS; initial catalog=testing; integrated security=true");
            var vResult=from stud in oDataContext.GetTable<student>() select stud;
            IDbCommand oIDbCommand = oDataContext.GetCommand(vResult);
            SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter();
            oSqlDataAdapter.SelectCommand = (SqlCommand)oIDbCommand;
            DataTable oDataTable = new DataTable("mydata");
            try
            {
                oIDbCommand.Connection.Open();
                oSqlDataAdapter.FillSchema(oDataTable, SchemaType.Source);
                oSqlDataAdapter.Fill(oDataTable);
                for(int iIndex=0;iIndex<oDataTable.Rows.Count;iIndex++)
                {
                    Console.WriteLine("Roll No = " + oDataTable.Rows[iIndex][0]);
                    Console.WriteLine("Name = " + oDataTable.Rows[iIndex][1]);
                    Console.WriteLine("Address = " + oDataTable.Rows[iIndex][2]);
                    Console.WriteLine("Age = " + oDataTable.Rows[iIndex][3]);
                    Console.WriteLine("Email = " + oDataTable.Rows[iIndex][4]);
                    Console.WriteLine("\n");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                oIDbCommand.Connection.Close();
            }
            Console.Read();
        }
    }


    [Table (Name ="student")]
    public class student
    {
        [Column(Name = "in_stud_id", IsPrimaryKey = true)]
        public int iStudId;
 
        [Column(Name = "nvc_name")]
        public string sName;
 
        [Column(Name = "nvc_address")]
        public string sAddress;
 
        [Column(Name = "in_age")]
        public int iAge;
 
        [Column(Name = "nvc_email")]
        public string sEmail;
    }
}

Updated 18-Sep-2014

Leave Comment

Comments

Liked By